package com.example.xmpic.mvp.present;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.aliyun.iot.aep.sdk.apiclient.callback.IoTResponse;
import com.example.common.mvp.BasePresenterImpl;
import com.example.common.mvp.onModelCallBack;
import com.example.common.utils.LiveDataBus;
import com.example.common.utils.SpUtil;
import com.example.xmpic.R;
import com.example.xmpic.mvp.contract.CameraIncreaseContract;
import com.example.xmpic.mvp.model.CameraModel;
import com.example.xmpic.mvp.model.CameraModelImpl;
import com.example.xmpic.ui.data.CameraBean;
import com.example.xmpic.ui.utils.CameraDaoUtil;

/**
 * @author skygge
 * @date 2020/5/26.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：添加摄像头业务逻辑
 */
public class CameraIncreasePresenter extends BasePresenterImpl<CameraIncreaseContract.IView> implements CameraIncreaseContract.IPresent {

    private Context mContext;
    private Handler mHandler;
    private CameraModelImpl mModel;

    public CameraIncreasePresenter(Context mContext) {
        this.mContext = mContext;
        mHandler = new Handler();
        mModel = new CameraModel(mHandler);
    }

    @Override
    public void refreshModifyUserInfo(String deviceId, String name) {
        CameraBean mCamera = CameraDaoUtil.getInstance().findCameraByCameraId(deviceId);
        if(mCamera != null){
            mView.hasExits();
            return;
        }
        String cameraName = SpUtil.getString(mContext, "camera");
        if (TextUtils.isEmpty(cameraName)){
            cameraName = name;
        }
        increaseCamera(deviceId, cameraName);
    }

    @Override
    public void increaseCamera(String deviceId, String name) {
        String mRoomId = SpUtil.getString(mContext, "modify_room");
        mModel.toAddCamera(deviceId, name, mRoomId, new onModelCallBack() {
            @Override
            public void onResponse(IoTResponse response) {
                if (response.getCode() == 200){
                    mHandler.post(()-> {
                        LiveDataBus.get().with("refresh_camera").setValue(mRoomId);
                        mView.bindSuccess();
                    });
                }else {
                    mHandler.post(()->mView.showToastMsg(response.getLocalizedMsg()));
                }
            }

            @Override
            public void onFailure(Exception e) {
                mHandler.post(()->mView.showToastMsg(mContext.getString(R.string.failed)));
            }
        });
    }

    @Override
    public void deleteCamera(String userId) {
        mModel.toDeleteCamera(userId, new onModelCallBack() {
            @Override
            public void onResponse(IoTResponse response) {
                if (response.getCode() == 200){
                    mHandler.post(()->mView.deleteSuccess());
                }else {
                    mHandler.post(()->mView.showToastMsg(response.getLocalizedMsg()));
                }
            }

            @Override
            public void onFailure(Exception e) {
                mHandler.post(()->mView.showToastMsg(mContext.getString(R.string.failed)));
            }
        });
    }
}
